<script>
	export default {
		//初始化机制，当用户已经登录过时触发
		onLaunch: function() {
			uni.getStorage({
				key: 'userInfo',
				success: (res) => {
					if (res.data) {
						this.$store.commit("setUserInfo", res.data)
						uni.showLoading({
							title: "登录中",
							mask: true
						})
						const url = '/appletUserqueryByOpenId?openId=' + res.data.openId
						let userInfo = {}
						uni.$u.http.get(url).then(result => {
							result.nickName = decodeURI(result.nickName)
							result.username = result.userName
							userInfo = result
							return uni.$u.http.post('/loginByUsernameAndOpenId', result)
						}).then(result => {
							if (result.token) {
								this.$store.commit("setUserInfo", userInfo)
								this.$store.commit("setUserToken", result.token)
								uni.setStorageSync("setUserToken", result.token)
								uni.hideLoading()
							}
						}).catch(err => {
							this.$store.commit("setUserInfo", {})
							uni.removeStorageSync("setUserToken")
							uni.hideLoading()
							uni.removeStorage({
								key: "userInfo"
							})

						})

					}
				}
			})
		},
		onShow: function() {

		},
		onHide: function() {

		},

	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/uview-ui/index.scss";
	@import "common/demo.scss";
</style>
